Segformer

understanding segformer code and structure
transformer
Author

Bowen

Published

December 2, 2021

import mmcv
import matplotlib.pyplot as plt
from fastcore.basics import *
from fastai.vision.all import *
from fastai.torch_basics import *
import warnings
warnings.filterwarnings("ignore")
import kornia
from kornia.constants import Resample
from kornia.color import *
from kornia import augmentation as K
import kornia.augmentation as F
import kornia.augmentation.random_generator as rg
from torchvision.transforms import functional as tvF
from torchvision.transforms import transforms
from torchvision.transforms import PILToTensor
from functools import partial
from timm.models.layers import trunc_normal_, DropPath
from timm.data import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD
from timm.models.vision_transformer import _cfg
from einops import rearrange
from timm.models.registry import register_model
set_seed(105)

some tips about how to transform segfomer code(from mmsegmentation) to hugging face https://github.com/NVlabs/SegFormer/issues/20

from transformers import SegformerModel, SegformerConfig

# Initializing a SegFormer nvidia/segformer-b0-finetuned-ade-512-512 style configuration
configuration = SegformerConfig()

# Initializing a model from the nvidia/segformer-b0-finetuned-ade-512-512 style configuration
model = SegformerModel(configuration)

# Accessing the model configuration
configuration = model.config
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
~/sharedData/swp/dlLabSwp/blog/myBlog/codeIdeas/transformers/src/transformers/file_utils.py in _get_module(self, module_name)
   2231         try:
-> 2232             return importlib.import_module("." + module_name, self.__name__)
   2233         except Exception as e:

~/miniconda3/envs/new/lib/python3.8/importlib/__init__.py in import_module(name, package)
    126             level += 1
--> 127     return _bootstrap._gcd_import(name[level:], package, level)
    128 

~/miniconda3/envs/new/lib/python3.8/importlib/_bootstrap.py in _gcd_import(name, package, level)

~/miniconda3/envs/new/lib/python3.8/importlib/_bootstrap.py in _find_and_load(name, import_)

~/miniconda3/envs/new/lib/python3.8/importlib/_bootstrap.py in _find_and_load_unlocked(name, import_)

~/miniconda3/envs/new/lib/python3.8/importlib/_bootstrap.py in _load_unlocked(spec)

~/miniconda3/envs/new/lib/python3.8/importlib/_bootstrap_external.py in exec_module(self, module)

~/miniconda3/envs/new/lib/python3.8/importlib/_bootstrap.py in _call_with_frames_removed(f, *args, **kwds)

~/sharedData/swp/dlLabSwp/blog/myBlog/codeIdeas/transformers/src/transformers/tokenization_utils.py in <module>
     26 from .file_utils import PaddingStrategy, TensorType, add_end_docstrings
---> 27 from .tokenization_utils_base import (
     28     ENCODE_KWARGS_DOCSTRING,

~/sharedData/swp/dlLabSwp/blog/myBlog/codeIdeas/transformers/src/transformers/tokenization_utils_base.py in <module>
     73 if is_tokenizers_available():
---> 74     from tokenizers import AddedToken
     75     from tokenizers import Encoding as EncodingFast

~/miniconda3/envs/new/lib/python3.8/site-packages/tokenizers/__init__.py in <module>
     78 
---> 79 from .tokenizers import (
     80     Tokenizer,

ImportError: /lib/x86_64-linux-gnu/libm.so.6: version `GLIBC_2.29' not found (required by /home/ubuntu/miniconda3/envs/new/lib/python3.8/site-packages/tokenizers/tokenizers.cpython-38-x86_64-linux-gnu.so)

The above exception was the direct cause of the following exception:

RuntimeError                              Traceback (most recent call last)
~/sharedData/swp/dlLabSwp/blog/myBlog/codeIdeas/transformers/src/transformers/file_utils.py in _get_module(self, module_name)
   2231         try:
-> 2232             return importlib.import_module("." + module_name, self.__name__)
   2233         except Exception as e:

~/miniconda3/envs/new/lib/python3.8/importlib/__init__.py in import_module(name, package)
    126             level += 1
--> 127     return _bootstrap._gcd_import(name[level:], package, level)
    128 

~/miniconda3/envs/new/lib/python3.8/importlib/_bootstrap.py in _gcd_import(name, package, level)

~/miniconda3/envs/new/lib/python3.8/importlib/_bootstrap.py in _find_and_load(name, import_)

~/miniconda3/envs/new/lib/python3.8/importlib/_bootstrap.py in _find_and_load_unlocked(name, import_)

~/miniconda3/envs/new/lib/python3.8/importlib/_bootstrap.py in _call_with_frames_removed(f, *args, **kwds)

~/miniconda3/envs/new/lib/python3.8/importlib/_bootstrap.py in _gcd_import(name, package, level)

~/miniconda3/envs/new/lib/python3.8/importlib/_bootstrap.py in _find_and_load(name, import_)

~/miniconda3/envs/new/lib/python3.8/importlib/_bootstrap.py in _find_and_load_unlocked(name, import_)

~/miniconda3/envs/new/lib/python3.8/importlib/_bootstrap.py in _load_unlocked(spec)

~/miniconda3/envs/new/lib/python3.8/importlib/_bootstrap_external.py in exec_module(self, module)

~/miniconda3/envs/new/lib/python3.8/importlib/_bootstrap.py in _call_with_frames_removed(f, *args, **kwds)

~/sharedData/swp/dlLabSwp/blog/myBlog/codeIdeas/transformers/src/transformers/models/__init__.py in <module>
     18 
---> 19 from . import (
     20     albert,

~/sharedData/swp/dlLabSwp/blog/myBlog/codeIdeas/transformers/src/transformers/models/layoutlm/__init__.py in <module>
     21 from ...file_utils import _LazyModule, is_tf_available, is_tokenizers_available, is_torch_available
---> 22 from .configuration_layoutlm import LAYOUTLM_PRETRAINED_CONFIG_ARCHIVE_MAP, LayoutLMConfig
     23 from .tokenization_layoutlm import LayoutLMTokenizer

~/sharedData/swp/dlLabSwp/blog/myBlog/codeIdeas/transformers/src/transformers/models/layoutlm/configuration_layoutlm.py in <module>
     18 
---> 19 from transformers import PretrainedConfig, PreTrainedTokenizer, TensorType
     20 

~/miniconda3/envs/new/lib/python3.8/importlib/_bootstrap.py in _handle_fromlist(module, fromlist, import_, recursive)

~/sharedData/swp/dlLabSwp/blog/myBlog/codeIdeas/transformers/src/transformers/file_utils.py in __getattr__(self, name)
   2221         elif name in self._class_to_module.keys():
-> 2222             module = self._get_module(self._class_to_module[name])
   2223             value = getattr(module, name)

~/sharedData/swp/dlLabSwp/blog/myBlog/codeIdeas/transformers/src/transformers/file_utils.py in _get_module(self, module_name)
   2233         except Exception as e:
-> 2234             raise RuntimeError(
   2235                 f"Failed to import {self.__name__}.{module_name} because of the following error (look up to see its traceback):\n{e}"

RuntimeError: Failed to import transformers.tokenization_utils because of the following error (look up to see its traceback):
/lib/x86_64-linux-gnu/libm.so.6: version `GLIBC_2.29' not found (required by /home/ubuntu/miniconda3/envs/new/lib/python3.8/site-packages/tokenizers/tokenizers.cpython-38-x86_64-linux-gnu.so)

The above exception was the direct cause of the following exception:

RuntimeError                              Traceback (most recent call last)
/tmp/ipykernel_11069/1946589403.py in <module>
----> 1 from transformers import SegformerModel, SegformerConfig
      2 
      3 # Initializing a SegFormer nvidia/segformer-b0-finetuned-ade-512-512 style configuration
      4 configuration = SegformerConfig()
      5 

~/miniconda3/envs/new/lib/python3.8/importlib/_bootstrap.py in _handle_fromlist(module, fromlist, import_, recursive)

~/sharedData/swp/dlLabSwp/blog/myBlog/codeIdeas/transformers/src/transformers/file_utils.py in __getattr__(self, name)
   2220             value = self._get_module(name)
   2221         elif name in self._class_to_module.keys():
-> 2222             module = self._get_module(self._class_to_module[name])
   2223             value = getattr(module, name)
   2224         else:

~/sharedData/swp/dlLabSwp/blog/myBlog/codeIdeas/transformers/src/transformers/file_utils.py in _get_module(self, module_name)
   2232             return importlib.import_module("." + module_name, self.__name__)
   2233         except Exception as e:
-> 2234             raise RuntimeError(
   2235                 f"Failed to import {self.__name__}.{module_name} because of the following error (look up to see its traceback):\n{e}"
   2236             ) from e

RuntimeError: Failed to import transformers.models.segformer because of the following error (look up to see its traceback):
Failed to import transformers.tokenization_utils because of the following error (look up to see its traceback):
/lib/x86_64-linux-gnu/libm.so.6: version `GLIBC_2.29' not found (required by /home/ubuntu/miniconda3/envs/new/lib/python3.8/site-packages/tokenizers/tokenizers.cpython-38-x86_64-linux-gnu.so)